<template>
  <view>
    <view class="goods-list">
      <view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
import { mapMutations,mapState } from 'vuex'
  export default {
    data() {
      return {
        // 请求参数对象
        queryObj: {
          query: '',
          categoryId: 0,
          pageNo: 1,
          pageSize: 10
        },
		// 请求参数对象
		queryMyObj: {
		  pageNo: 1,
		  pageSize: 10
		},
        goodsList: [],
        total: 0,
        // 节流阀
        isloading: false
      };
    },
	computed: {
	  ...mapState('m_user', ['userinfo']),
	  ...mapState('m_user', ['userId']),
	  ...mapState('m_dept', ['deptId']),
	  ...mapState('m_dept', ['dept'])
	},
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.categoryId = options.id || ''
	  //1是从商品类型的地方进来的，2是从我的里面自己发布的商品进来的 10是我收藏的商品
	if(options.flag==1){
		 this.getGoodsList()
	}else if(options.flag==2){
		 this.getMyGoodsList()
	}else{
		this.getMyCollectionsList()
	}
     
    },
    methods: {
      // 获取商品列表数据的方法
     //获取商品列表
     async getGoodsList() {
     		   // ** 打开节流阀
		this.isloading = true
		 await uni.request({
			 url: uni.$http.baseUrl+'/api/mobileMall/getMallList',
			 data: {
				deptId: this.deptId,
				followId: this.userId,
				categoryId:  this.queryObj.categoryId,
				// 页码值
				pageNo: this.queryObj.pageNo,
				// 每页显示多少条数据
				pageSize: this.queryObj.pageSize,
			 },
			method: 'POST',
			 header: {
				   // "Content-Type": "application/json",
				   "Content-Type": "application/x-www-form-urlencoded"
				 },
			 success: (res) => {
					 // ** 关闭节流阀
				this.isloading = false
				this.goodsList=[...this.goodsList,...res.data.data]
				console.log(this.goodsList)
			 },
			fail: (res) =>{
				if (res.statusCode !== 200) return uni.$showMsg()
			}
		 });
      
      },
	  
	  //我发布的商品列表
	  async getMyGoodsList() {
	  		   // ** 打开节流阀
	  		this.isloading = true
	  		 await uni.request({
	  			 url: uni.$http.baseUrl+'/api/mobileMall/getMyPublishList',
	  			 data: {
	  				userId: this.userId,
	  				// 页码值
	  				pageNo: this.queryMyObj.pageNo,
	  				// 每页显示多少条数据
	  				pageSize: this.queryMyObj.pageSize,
	  			 },
	  			method: 'POST',
	  			 header: {
	  				  //  "Content-Type": "application/json",
	  				   "Content-Type": "application/x-www-form-urlencoded"
	  				 },
	  			 success: (res) => {
	  					 // ** 关闭节流阀
	  				this.isloading = false
	  				this.goodsList=[...this.goodsList,...res.data.data]
	  				console.log(this.goodsList)
	  			 },
	  			fail: (res) =>{
	  				if (res.statusCode !== 200) return uni.$showMsg()
	  			}
	  		 });
	   
	   },
	   //我收藏的商品列表
	   async getMyCollectionsList() {
	   		   // ** 打开节流阀
	   		this.isloading = true
	   		 await uni.request({
	   			 url: uni.$http.baseUrl+'/api/mobileMall/getMyCollectionsList',
	   			 data: {
	   				userId: this.userId,
	   				// 页码值
	   				pageNo: this.queryMyObj.pageNo,
	   				// 每页显示多少条数据
	   				pageSize: this.queryMyObj.pageSize,
	   			 },
	   			method: 'POST',
	   			 header: {
	   				  //  "Content-Type": "application/json",
	   				   "Content-Type": "application/x-www-form-urlencoded"
	   				 },
	   			 success: (res) => {
	   					 // ** 关闭节流阀
	   				this.isloading = false
	   				this.goodsList=[...this.goodsList,...res.data.data]
	   				console.log(this.goodsList)
	   			 },
	   			fail: (res) =>{
	   				if (res.statusCode !== 200) return uni.$showMsg()
	   			}
	   		 });
	    
	    },
      gotoDetail(goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?id=' + goods.id
        })
      }
    },
    onReachBottom() {
      if (this.queryObj.pageNo * this.queryObj.pageSize >= this.total) return uni.$showMsg('数据加载完毕!')

      if (this.isloading) return

      // 让页码值自增+1
      this.queryObj.pageNo++
      this.getGoodsList()
    },
    onPullDownRefresh() {
      // 重置关键数据
      this.queryObj.pageNo = 1
      this.total = 0
      this.isloading = false
      this.goodsList = []

      // 重新发起数据请求
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
